package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;

    /**
     * 登录功能
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R<Employee> selectOne(@RequestBody Employee employee, HttpSession session){
        //根据用户名查询是否存在
        Employee emp = employeeService.query().eq("username", employee.getUsername()).one();
        if (emp==null) {
            return R.error("用户名不存在");
        }
        //判断密码是否错误
        String password1 = employee.getPassword();
        String password2 = emp.getPassword();
        //加密
        String password3 = DigestUtils.md5Hex(password1);
        boolean contains = StringUtils.equals(password2, password3);

        if (!contains) {
            return R.error("密码错误");
        }
        //判断账号状态
        if (emp.getStatus()==0) {
            return R.error("账号已禁用");
        }
        session.setAttribute("employee",emp.getId());
        return R.success(emp);
    }

    /**
     * 退出功能
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> a(HttpServletRequest request){
        //删除 session
        request.getSession().invalidate();
        return R.success("退出成功");
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody Employee employee,HttpSession Session){
        //这里有可能抛异常 因为用户名不可重复 配置异常处理类
        employee.setPassword(DigestUtils.md5Hex("123456"));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) Session.getAttribute("employee");
        employee.setUpdateUser(empId);
        employee.setCreateUser(empId);
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page,Integer pageSize,String name){
        //创建分页对象
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        employeeService.query().like(StringUtils.isNotBlank(name),"name",name)
                .orderByDesc("update_time")
                .page(pageInfo);
        return R.success(pageInfo);
    }
    /**
     * 修改状态
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
        Long employee1 = (Long) session.getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(employee1);
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    /**
     * 根据id回显数据
     */
    @GetMapping("/{id}")
    public R<Employee> updateById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return  R.success(employee);
    }
}
